<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户信息
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        size="primary"
                        class="btn-add"
                        @click="handleAdd()"
                        style="margin-left: 20px"
                >添加
                </el-button>
                <!--添加用户-->
                <el-dialog
                        title="添加用户"
                        :visible.sync="dialogVisible"
                        width="40%">
                    <el-form
                            :model="user"
                            ref="roleForm"
                            label-width="150px" size="small">
                        <el-form-item label="用户名称：">
                            <el-input v-model="user.username" style="width: 250px"></el-input>
                        </el-form-item>
                        <el-form-item label="密码：">
                            <el-input v-model="user.password" style="width: 250px"></el-input>
                        </el-form-item>
                        <el-form-item label="email：">
                            <el-input v-model="user.email" style="width: 250px"></el-input>
                        </el-form-item>
                        <el-form-item label="电话号码：">
                            <el-input v-model="user.phone" style="width: 250px"></el-input>
                        </el-form-item>
                        <el-form-item label="性别：">
                            <el-radio v-model="user.sex" label="男">男</el-radio>
                            <el-radio v-model="user.sex" label="女">女</el-radio>
                        </el-form-item>
                        <el-form-item label="state：">
                            <el-input v-model="user.state" style="width: 250px"></el-input>
                        </el-form-item>
                        <el-form-item label="description：">
                            <el-input v-model="user.description" style="width: 250px"></el-input>
                        </el-form-item>
                        <el-form-item label="roleId：">
                            <el-input v-model="user.roleId" style="width: 250px"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                        <el-button type="primary" @click="" size="small">确 定</el-button>
                    </span>
                </el-dialog>
                <!--添加用户END-->
                <div style="float: right">
                    <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search">搜索</el-button>
                </div>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection"  align="center"></el-table-column>
                <el-table-column prop="id" label="ID"  align="center"></el-table-column>
                <el-table-column prop="username" label="用户名" ></el-table-column>
                <el-table-column prop="sex" label="性别" ></el-table-column>
                <el-table-column prop="phone" label="电话号码" ></el-table-column>
                <el-table-column prop="email" label="邮箱" ></el-table-column>
                <el-table-column label="更新时间" >{{this.form.updated | formatDate}}</el-table-column>
                <el-table-column prop="description" label="身份" ></el-table-column>
                <el-table-column label="操作"  align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleSelectRole(scope.$index, scope.row)"
                        >分配角色
                        </el-button>
                        <el-button
                                v-if="scope.row.state == 1"
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleStopUser(scope.$index, scope.row)"
                        >停用
                        </el-button>
                        <el-button
                                v-else
                                type="text"
                                icon="el-icon-delete"
                                class="yellow"
                                @click="handleStartUser(scope.$index, scope.row)"
                        >启用
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelUser(scope.$index, scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--分配角色-->
            <el-dialog
                    title="分配角色"
                    :visible.sync="allocDialogVisible"
                    width="30%">
                <el-select multiple placeholder="请选择" size="small" style="width: 80%">
                    <el-option
                            :value="allRoleList">
                    </el-option>
                </el-select>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="allocDialogVisible = false" size="small">取 消</el-button>
                    <el-button type="primary" @click="handleAllocDialogConfirm()" size="small">确 定</el-button>
                </span>
            </el-dialog>
            <!--分配角色END-->
            <!--分页-->
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="tableData.length"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
            <!--分页-->
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-input v-model="form.sex"></el-input>
                </el-form-item>
                <el-form-item label="电话号码">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input :value="this.form.created  | formatDate"></el-input>
                </el-form-item>
                <el-form-item label="更新时间">
                    <el-input :value="this.form.updated  | formatDate"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {GetUserList, userStop, userStart, updateUser, delUser} from '../../../api/user';

    export default {
        name: 'basetable',
        data() {
            return {
                dialogVisible: false,//添加用户弹窗
                allocDialogVisible: false,//分配角色弹窗
                query: {
                    address: '',
                    name: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,
                id: -1,
                allRoleList: ['1','2'],
                //添加用户表单
                user: {
                    username: '',
                    password: '',
                    email: '',
                    phone: '',
                    sex: '',
                    state: '',
                    description: '',
                    roleId: ''
                }
            };
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                GetUserList().then(res => {
                    this.tableData = res.data.data
                });
            },
            // 停用用户
            handleStopUser(index, row) {
                this.$confirm('确定要停用该用户吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        userStop(row.id).then(res => {
                            if(res.data.result === null){
                                this.tableData = [];
                                this.getData();
                                this.$message.success('停用成功');
                            }else{
                                this.$message.error('停用失败');
                                this.$router.push('/500')
                            }

                        })
                    })
                    .catch(() => {
                    });
            },
            // 启用用户
            handleStartUser(index, row) {
                this.$confirm('确定要启用该用户吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        userStart(row.id).then(res => {
                            if (res.data.result === null){
                                this.tableData = [];
                                this.getData();
                                this.$message.success('启用成功');
                            }else{
                                this.$message.error('启用失败');
                                this.$router.push('/500')
                            }

                        })
                    })
                    .catch(() => {
                    });
            },
            //添加用户
            handleAdd() {
                this.dialogVisible = true;
            },
            //删除用户
            handleDelUser(index, row){
                this.$confirm('确定要删除该用户吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        delUser(row.id).then(res => {
                            if (res.data.result === null){
                                this.tableData = [];
                                this.getData();
                                this.$message.success('删除成功');
                            }else{
                                this.$message.error('删除失败');
                                this.$router.push('/500')
                            }
                        })
                    })
                    .catch(() => {
                    });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 编辑操作
            handleEdit(index, row) {
                console.log('0-0', row);
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            //分配角色
            handleSelectRole(index, row) {
                console.log(row)
                this.allocDialogVisible = true;
            },
            // 保存编辑
            saveEdit() {
                this.editVisible = false;
                updateUser(this.form).then(res => {
                    console.log(res);
                })
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            //
            handleAllocDialogConfirm() {
                this.$confirm('是否要确认?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                })
            },
        },
        created() {
            this.getData();
        },
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .yellow {
        color: #ffa340;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
